<template>
	<view class="my-path-list">
		<view class="path-list" v-for="(item,index) in list" :key="index" @tap="toAddPath(index)">
			<view class="" @tap="goConfirOrder(item)">
				<view class="path-item">
					<view class="item-main">
						<view class="item-name">
							{{item.name}}
						</view>
						<view class="">
							{{item.tel}}
						</view>
					</view>
					<view class="item-main">
						<template v-if="item.isDefault==='1'">
							<view class="active">
							默认
						</view>
						</template>
						
						<view class="">
							{{item.province}}{{item.city}}{{item.district}}{{item.address}}
						</view>
						
					</view>
				</view>
			</view>
			<mylines></mylines>
		</view>
		<view class="add-path">
			<view class="add-path-btn" @tap="gomyaddpath">
				新增地址
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState,mapMutations} from 'vuex'
	import $http from '@/common/api/request.js'
	import mylines from '@/components/common/Lines.vue'
	export default {
		data() {
			return {
				isSelectPath:false
			};
		},
		methods:{
			// 请求收货地址接口
			...mapMutations(['__initAddressList']),
			__initAddress(){
				$http.request({
					url: "/selectAddress",
					method:"POST",
					header:{
						token:true
						}
				}).then((res) => {
					this.__initAddressList(res)
				}).catch(() => {
					uni.showToast({
						title:"请求地址接口失败",
						icon:"none",
						mask:true
					})
				})
			},
			gomyaddpath(){
				
				uni.navigateTo({
					url:"/pages/my-add-path/my-add-path"
				})
				
			},
			toAddPath(index){
				if(! this.isSelectPath){
					let pathObj = JSON.stringify({
					index:index,
					item:this.list[index]
				})
				uni.navigateTo({
					url:`/pages/my-add-path/my-add-path?data=${pathObj}`
				})
				}
				
			},
			goConfirOrder(item){
				if(this.isSelectPath){
					uni.$emit('selectPathItem',item);
					uni.navigateBack({
						delta:1
					})
				}
			}
		},
		computed:{
			...mapState({
				 list:state=>state.path.list
			})
		},
		onNavigationBarButtonTap() {
			uni.navigateBack({
				delta:1
			})
		},
		components:{
			mylines
		},
		onLoad(e) {
			if(e.type === 'selectedPath'){
				this.isSelectPath=true;
			}
			
		},
		onShow() {
			this.__initAddress();
		}
	}
</script>

<style>
.add-path{
	width: 100%;
	display: flex;
	justify-content: center;
	
}
.add-path-btn{
	border: 2rpx solid #49BDFB;
	color: #49BDFB;
	border-radius: 30rpx;
	padding: 6rpx 60rpx;
}
.path-list{
	padding-left: 20rpx;
}
.path-item{
	padding: 10rpx;
	border-buttom: 2rpx solid #cccccc;
}
.item-main{
	display: flex;
	align-items: center;
}
.item-name{
	padding: 10rpx;
}
.active{
	padding:10rpx 10rpx;
	background-color: #49BDFB;
	color: #FFFFFF;
	border-radius: 26rpx;
	margin-right: 10rpx;
}
</style>
